﻿/*************************************************************************/
/** Copyright.															**/
/** FileName: RTCNotice.c												**/
/** Author: Polarix														**/
/** Version: 1.0.0.0													**/
/** Description: HMI demo for notice box interface and refresh screen.	**/
/*************************************************************************/
//=======================================================================//
//= Include files.													    =//
//=======================================================================//
#include <math.h>
#include "app_gui.h"
#include "HMI_Engine.h"
#include "SGUI_Basic.h"
#include "SGUI_FontResource.h"
#include "Resource.h"
#include "SGUI_Text.h"
#include "HAL.h"
#include "Touch.h"
/*
#include "fingerprint.h"
#include "nvs_flash.h"
#include "motor.h"
#include "fingerprint.h"
*/

#define PI   3.14

//=======================================================================//
//= User Macro definition.											    =//
//=======================================================================//
#define     BUTTON_STA_NONE                 0x00
#define     BUTTON_STA_NUMA_DOWN    0x01

//=======================================================================//
//= Static function declaration.									    =//
//=======================================================================//
static HMI_ENGINE_RESULT	HMI_StandBy_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT	HMI_StandBy_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT	HMI_StandBy_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID);
static HMI_ENGINE_RESULT	HMI_StandBy_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID);
static void DrawClockSurface(SGUI_SCR_DEV* pstDeviceIF,SGUI_INT iCx, SGUI_INT iCy, SGUI_INT iRadius);
static void DrawClock(SGUI_SCR_DEV* pstDeviceIF, int hour, int minute, int second);
static void DrawTime(SGUI_SCR_DEV* pstDeviceIF,SGUI_INT iCx, SGUI_INT iCy,int hour, int minute, int second);
static void DrawDate(SGUI_SCR_DEV* pstDeviceIF,SGUI_INT iCx, SGUI_INT iCy,int year, int month, int day);
//=======================================================================//
//= Static variable declaration.									    =//
//=======================================================================//
HMI_SCREEN_ACTION		s_stStandByPaintActions =		{	NULL,
															HMI_StandBy_Prepare,
															HMI_StandBy_RefreshScreen,
															HMI_StandBy_ProcessEvent,
															HMI_StandBy_PostProcess
														};
//=======================================================================//
//= Global variable declaration.									    =//
//=======================================================================//
HMI_SCREEN_OBJECT       g_stHMI_StandBy =		{	HMI_SCREEN_ID_STANDBY,
															&s_stStandByPaintActions
														};

static SGUI_UINT8   button_status;
static SGUI_UINT8   key_value;
//=======================================================================//
//= Function define.										            =//
//=======================================================================//
HMI_ENGINE_RESULT HMI_StandBy_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
	if(NULL != pstDeviceIF->fnClear)
	{
		pstDeviceIF->fnClear();
	}
	else
	{
		SGUI_Basic_DrawRectangle(pstDeviceIF, 0, 0, pstDeviceIF->stSize.iWidth, pstDeviceIF->stSize.iHeight, SGUI_COLOR_BKGCLR, SGUI_COLOR_BKGCLR);
	}

  //  DrawClockSurface(pstDeviceIF,32,32,26);

	HMI_StandBy_RefreshScreen(pstDeviceIF, pstParameters);

//	OnBoard_SendMsg(halTaskID, TIME_UPD, CMD_ON, NULL);
//	FP_SET_WorkState(FP_Workstate_MatchFP);
//	PFIC_EnableIRQ(GPIO_B_IRQn);
//    OnBoard_SendMsg(halTaskID, NFC_READ, CMD_ON, NULL);
//
//    button_status = BUTTON_STA_NONE;
//    BLK_State_Set(BLK_STATE_STANDY);

	return HMI_RET_NORMAL;
}
extern const SGUI_FONT_RES GB2312_FZXS12;

HMI_ENGINE_RESULT HMI_StandBy_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    /*----------------------------------*/
	/* Variable Declaration				*/
	/*----------------------------------*/
	SGUI_INT                iPaintBkgIdx;

	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
   // DrawClock(pstDeviceIF,0,0,0);

   //  DrawDate(pstDeviceIF,68,20,0,0,0);
     //DrawTime(pstDeviceIF,0,0,12,12,12);
     SGUI_RECT                   stIconlayout;
     SGUI_SystemIF_MemorySet(&stIconlayout, 0x00, sizeof(SGUI_RECT));
     SGUI_POINT                  stContentInnerPos;

     stIconlayout.iX = 2;
     stIconlayout.iY = GB2312_FZXS12.iHeight+2+3;
     stIconlayout.iHeight =50;
     stIconlayout.iWidth = 50;

     SGUI_RECT                   stTextlayout;
     SGUI_SystemIF_MemorySet(&stTextlayout, 0x00, sizeof(SGUI_RECT));
     SGUI_POINT                  stTexTInnerPos;

     stTexTInnerPos.iX = 0;
     stTexTInnerPos.iY = 0;
     stTextlayout.iX = stIconlayout.iWidth+10;
     stTextlayout.iY = (GB2312_FZXS12.iHeight+2)+5;
     stTextlayout.iHeight = (GB2312_FZXS12.iHeight+2);
     stTextlayout.iWidth = 100;

    SGUI_Text_DrawText(pstDeviceIF, "HELL0", &GB2312_FZXS12, &stTextlayout, &stTexTInnerPos, SGUI_DRAW_NORMAL);
     //SGUI_Text_DrawASCIICharacter(pstDeviceIF, SGUI_CHAR cChar, const SGUI_FONT_RES* pstFontRes, const SGUI_RECT* pstDisplayArea, const SGUI_POINT* pstInnerPos, SGUI_DRAW_MODE eFontMode)
     return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_StandBy_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID)
{
	/*----------------------------------*/
	/* Variable Declaration				*/
	/*----------------------------------*/
	HMI_ENGINE_RESULT           eProcessResult;
	SGUI_INT					iProcessAction;
	/*----------------------------------*/
	/* Initialize						*/
	/*----------------------------------*/
	eProcessResult =			HMI_RET_NORMAL;
	iProcessAction =			HMI_DEMO_PROC_NO_ACT;

	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
	if(pstEvent->iType == EVENT_TYPE_ACTION)
	{
		if(EVENT_ID_KEY_PRESS == pstEvent->iID)
		{
	        KEY_PRESS_EVENT*            pstKeyEvent;

	        pstKeyEvent =               (KEY_PRESS_EVENT*)pstEvent;
	        // Check event is valid.
	        if(SGUI_FALSE == HMI_EVENT_SIZE_CHK(*pstKeyEvent, KEY_PRESS_EVENT))
	        {
	            // Event data is invalid.
	            eProcessResult = HMI_RET_INVALID_DATA;
	        }
	        key_value = pstKeyEvent->Data.uiKeyValue;
		    if(key_value == KEY_NUMA)			  iProcessAction = HMI_DEMO_PROC_CANCEL;
		    else if(key_value == KEY_NUMP)    iProcessAction = HMI_DEMO_PROC_CONFIRM;
		    else                                                iProcessAction = HMI_DEMO_PROC_NORMAL;
		}
        else if(EVENT_ID_FP_PRESSS == pstEvent->iID)
        {
            FP_PRESS_EVENT*            pstFpEvent;
            pstFpEvent =               (FP_PRESS_EVENT*)pstEvent;
            // Check event is valid.
            if(SGUI_FALSE == HMI_EVENT_SIZE_CHK(*pstFpEvent, FP_PRESS_EVENT))
            {
                // Event data is invalid.
                eProcessResult = HMI_RET_INVALID_DATA;
            }


            SGUI_INT Fp_id = pstFpEvent->Data.FP_id;
            SGUI_INT Fp_conditance = pstFpEvent->Data.Match_ratio;
            SGUI_UINT8 Fp_times = pstFpEvent->Data.Match_step;

//            if(Fp_conditance >= 90)
//            {
//                SGUI_CHAR FP_ID_CHAR[5] = {0};
//                sprintf((char *)FP_ID_CHAR,"%04d",Fp_id);
//                  OnBoard_SendMsg(halTaskID, TIME_UPD, CMD_OFF, NULL);
//                  OnBoard_SendMsg(halTaskID, NFC_READ, CMD_OFF, NULL);
//                  PFIC_DisableIRQ(GPIO_B_IRQn);
//                  HMI_SwitchScreen(HMI_SCREEN_ID_OPENDOOR,(const void*)FP_ID_CHAR);
//                  Motor_SetAction();
//                  extern void FP_Sleep_process(void);
//                  FP_Sleep_process();
//                  FP_WorkstateSet (FP_Workstate_Standby);
//            }
//            else if(Fp_times == 2)
//            {
//                  PFIC_DisableIRQ(GPIO_B_IRQn);
//                  HMI_SwitchScreen(HMI_SCREEN_ID_OPENDOOR,NULL);
//                  Motor_SetAction();
//                  extern void FP_Sleep_process(void);
//                  FP_Sleep_process();
//                  FP_WorkstateSet (FP_Workstate_Standby);
//            }
        }
        else if(EVENT_ID_TIMER == pstEvent->iID)
        {
            button_status = BUTTON_STA_NONE;
        }
	}
	else if(pstEvent->iType == EVENT_TYPE_DATA)
	{
	    if(EVENT_ID_TIMER == pstEvent->iID)
	    {
	        TIME_UPDATE_EVENT*            pstTimeEvent;

	        pstTimeEvent =               (TIME_UPDATE_EVENT*)pstEvent;
	        SGUI_Basic_DrawCircle(pstDeviceIF, 32, 32, 26-4, SGUI_COLOR_TRANS, SGUI_COLOR_BKGCLR);
	        DrawClock(pstDeviceIF,(pstTimeEvent->Data.hour>12)? (pstTimeEvent->Data.hour - 12) : pstTimeEvent->Data.hour
	            ,pstTimeEvent->Data.min,pstTimeEvent->Data.sec);

	        DrawDate(pstDeviceIF,68,20,pstTimeEvent->Data.year,pstTimeEvent->Data.month,pstTimeEvent->Data.day);
	        DrawTime(pstDeviceIF,80,36,pstTimeEvent->Data.hour,pstTimeEvent->Data.min,pstTimeEvent->Data.sec);

	    }
	    else if(EVENT_ID_NFC_READ == pstEvent->iID)
        {
            CARD_READ_EVENT*            pstCardEvent;
            SGUI_UINT8      i;
            pstCardEvent =               (CARD_READ_EVENT*)pstEvent;
//            for( i = 0; i < MAX_CARD_NUM; i++)
//            {
//                 if(TRUE == tmos_memcmp(nvs_flash_info.card_user[i].c_id, pstCardEvent->Data.c_id, 4))
//                 {
//                     SGUI_CHAR CARD_ID_CHAR[5] = {0};
//                     sprintf((char *)CARD_ID_CHAR,"%04d",nvs_flash_info.card_user[i].user_id);
//                     OnBoard_SendMsg(halTaskID, TIME_UPD, CMD_OFF, NULL);
//                     OnBoard_SendMsg(halTaskID, NFC_READ, CMD_OFF, NULL);
//                     PFIC_DisableIRQ(GPIO_B_IRQn);
//                     HMI_SwitchScreen(HMI_SCREEN_ID_OPENDOOR,(const void*)CARD_ID_CHAR);
//                     Motor_SetAction();
//                     break;
//                 }
//            }
//
//            if(i == MAX_CARD_NUM)
//            {
//                HMI_SwitchScreen(HMI_SCREEN_ID_OPENDOOR,NULL);
//                Motor_SetAction();
//            }
        }
	    else if(EVENT_ID_BLE == pstEvent->iID)
	    {
	        DATA_EVENT*            pstBleEvent;
	        pstBleEvent =               (DATA_EVENT*)pstEvent;
//	        if(pstBleEvent->Data.iValue == 0x01)
//	        {
//                SGUI_CHAR BLE_ID_CHAR[5] = {0};
//                sprintf((char *)BLE_ID_CHAR,"%04d",0000);
//                OnBoard_SendMsg(halTaskID, TIME_UPD, CMD_OFF, NULL);
//                OnBoard_SendMsg(halTaskID, NFC_READ, CMD_OFF, NULL);
//                PFIC_DisableIRQ(GPIO_B_IRQn);
//                HMI_SwitchScreen(HMI_SCREEN_ID_OPENDOOR,(const void*)BLE_ID_CHAR);
//                Motor_SetAction();
//	        }
	    }
	}

	if(NULL != piActionID)
	{
		*piActionID = iProcessAction;
	}

	return eProcessResult;
}

HMI_ENGINE_RESULT HMI_StandBy_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID)
{
	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/

	if(HMI_PROCESS_SUCCESSFUL(eProcResult))
	{
	    if(HMI_DEMO_PROC_CANCEL == iActionID)
		{
		   // OnBoard_SendTime(halTaskID, TIME_CD, CMD_ON, 1000);
		    button_status = BUTTON_STA_NUMA_DOWN;
		}
		else if(HMI_DEMO_PROC_CONFIRM == iActionID)
		{
		    if(button_status == BUTTON_STA_NUMA_DOWN)
		    {
                  HMI_SwitchScreen(HMI_SCREEN_ID_MENU, NULL);
                //  PFIC_DisableIRQ(GPIO_B_IRQn);
               //   OnBoard_SendMsg(halTaskID, NFC_READ, CMD_OFF, NULL);
               //   OnBoard_SendMsg(halTaskID, TIME_UPD, CMD_OFF, NULL);
               //   OnBoard_SendTime(halTaskID, TIME_CD, CMD_OFF, 1);
		    }
		}
		else if(HMI_DEMO_PROC_NORMAL == iActionID)
		{
            SGUI_UINT8      pin_mode = VARIABLE_PIN_MODE_OPEN_DOOR;
            HMI_SwitchScreen(HMI_SCREEN_ID_PIN, &pin_mode);
           // PFIC_DisableIRQ(GPIO_B_IRQn);
           // OnBoard_SendMsg(halTaskID, NFC_READ, CMD_OFF, NULL);
          //  OnBoard_SendMsg(halTaskID, TIME_UPD, CMD_OFF, NULL);
		}
	}



	return HMI_RET_NORMAL;
}


static void DrawClockSurface(SGUI_SCR_DEV* pstDeviceIF,SGUI_INT iCx, SGUI_INT iCy, SGUI_INT iRadius)
{
    SGUI_Basic_DrawCircle(pstDeviceIF, iCx, iCx, iRadius, SGUI_COLOR_FRGCLR, SGUI_COLOR_TRANS);
    SGUI_Basic_DrawCircle(pstDeviceIF, iCx, iCx, iRadius+4, SGUI_COLOR_FRGCLR, SGUI_COLOR_TRANS);
    double arc_l;
    SGUI_INT arc,x1_arc,y1_arc,x2_arc,y2_arc;
    for(SGUI_INT i = 0 ; i < 12 ; i++)
    {
             arc = i*5;
             arc_l = arc * 2 * PI / 60;
//             x1_arc = 32 + (SGUI_INT)(iRadius * sin(arc_l));
//             y1_arc = 32 - (SGUI_INT)(iRadius * cos(arc_l));
//             x2_arc = 32 + (SGUI_INT)((iRadius-4) * sin(arc_l));
//             y2_arc = 32 - (SGUI_INT)((iRadius-4) * cos(arc_l));
             // 画刻度
             SGUI_Basic_DrawLine(pstDeviceIF, x1_arc,y1_arc,x2_arc,y2_arc,SGUI_COLOR_FRGCLR);
    }
}

static void DrawClock(SGUI_SCR_DEV* pstDeviceIF, int hour, int minute, int second)
{
    double a_hour, a_min, a_sec;      // 时、分、秒针的弧度值
    SGUI_INT x_hour, y_hour, x_min, y_min, x_sec, y_sec; // 时、分、秒针的末端位置
    // 计算时、分、秒针的弧度值
    a_sec = second * 2 * PI / 60;
    a_min = minute * 2 * PI / 60 + a_sec / 60;
    a_hour= hour * 2 * PI / 12 + a_min / 12;
    // 计算时、分、秒针的末端位置
    x_sec = 32 + 3;//(SGUI_INT)(20 * (sin)(a_sec));
    y_sec = 32 - 19;//(SGUI_INT)(20 * cos(a_sec));
    x_min = 32 + 2;//(SGUI_INT)(15 * (sin)(a_min));
    y_min = 32 - 14;//(SGUI_INT)(15 * cos(a_min));
    x_hour= 32 + 2;//(SGUI_INT)(10 * sin(a_hour));
    y_hour= 32 - 10;//(SGUI_INT)(10 * cos(a_hour));
    // 画时针
    SGUI_Basic_DrawLine(pstDeviceIF, x_hour,y_hour,32,32,SGUI_COLOR_FRGCLR);
    // 画分针
    SGUI_Basic_DrawLine(pstDeviceIF, x_min,y_min,32,32,SGUI_COLOR_FRGCLR);
    // 画秒针
    SGUI_Basic_DrawLine(pstDeviceIF, x_sec,y_sec,32,32,SGUI_COLOR_FRGCLR);

}

static void DrawTime(SGUI_SCR_DEV* pstDeviceIF,SGUI_INT iCx, SGUI_INT iCy,int hour, int minute, int second)
{
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    SGUI_RECT                   stDisplayArea;
    SGUI_POINT                  stInnerPos;

    SGUI_SZSTR  time_char[10];

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    stInnerPos.iX =         0;
    stInnerPos.iY =         0;

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    stDisplayArea.iX = iCx;
    stDisplayArea.iY = iCy;
    stDisplayArea.iWidth = 100;
    stDisplayArea.iHeight = 16+1;
    sprintf((char *)time_char,"%02d:%02d:%02d",hour,minute,second);
    SGUI_Text_DrawText(pstDeviceIF, (SGUI_CSZSTR)time_char, &SGUI_DEFAULT_FONT_16, &stDisplayArea, &stInnerPos, SGUI_DRAW_NORMAL);

}

static void DrawDate(SGUI_SCR_DEV* pstDeviceIF,SGUI_INT iCx, SGUI_INT iCy,int year, int month, int day)
{
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    SGUI_RECT                   stDisplayArea;
    SGUI_POINT                  stInnerPos;

    SGUI_SZSTR  date_char[10];

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    stInnerPos.iX =         0;
    stInnerPos.iY =         0;

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    stDisplayArea.iX = iCx;
    stDisplayArea.iY = iCy;
    stDisplayArea.iWidth = 62;
    stDisplayArea.iHeight = 12+1;
    sprintf((char *)date_char,"%04d/%02d/%02d",year,month,day);
    SGUI_Text_DrawText(pstDeviceIF, (SGUI_CSZSTR)date_char, &SGUI_DEFAULT_FONT_12, &stDisplayArea, &stInnerPos, SGUI_DRAW_NORMAL);

}
